﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditService : AdminPageBase
    {
        private ServiceInfo _serviceInfo = new ServiceInfo();
        private readonly ServiceController _serviceController = new ServiceController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ServiceId"]))
                _serviceInfo = _serviceController.GetService(int.Parse(Request.QueryString["ServiceId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();
            LoadLocation();
            LoadPartner();

            if (_serviceInfo.ServiceId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListService.aspx?xml=Service");
        }

        private void SaveData()
        {
            _serviceInfo.ServiceName = txtServiceName.Text;
            _serviceInfo.LocationId = int.Parse(ddlLocation.SelectedValue);
            _serviceInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _serviceInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            if (_serviceInfo.ServiceId == -1)
            {
                _serviceInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _serviceInfo.CreatedDate = DateTime.Now;
                _serviceController.InsertService(_serviceInfo);
            }
            else
            {
                _serviceInfo.UpdatedBy = UserId;
                _serviceInfo.UpdatedDate = DateTime.Now;
                _serviceController.UpdateService(_serviceInfo);
            }
            Response.Redirect("/Pages/ListService.aspx?xml=Service");
        }

        private void RenderForm()
        {
            txtServiceName.Text = _serviceInfo.ServiceName;
            ddlLocation.SelectedValue = _serviceInfo.LocationId.ToString();
            ddlStatus.SelectedValue = _serviceInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _serviceInfo.CreatedBy.ToString();
            ddlPartner.SelectedValue = _serviceInfo.PartnerId.ToString();
        }

        private void LoadLocation()
        {
            ddlLocation.DataSource =
                new LocationController().ListLocation()
                    .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                    .OrderBy(p => p.LocationName);
            ddlLocation.DataTextField = "LocationName";
            ddlLocation.DataValueField = "LocationId";
            ddlLocation.DataBind();

            ddlLocation.Items.Insert(0, new ListItem("--- Chọn địa điểm ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadPartner()
        {
            ddlPartner.DataSource =
               new PartnerController().ListPartner()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.PartnerName);
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn đối tác ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}